package com.water.app.Controller.Goods;

import com.water.app.*;
import com.water.app.Exceptions.DeleteException;
import com.water.app.Exceptions.isPassException;
import com.water.app.interfaces.Goods.GoodsService;
import com.water.app.interfaces.Goods.shopService;
import com.water.app.interfaces.account.userService;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/shop")
public class shopController {

    private static final Logger log = LoggerFactory.getLogger(shopController.class);
    @Resource
    private shopService shopService;
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private GoodsService goodsService;

    @Resource
    private userService userService;

    //查询用户的某一个店铺的详情
    @GetMapping("/{shopId}")
    public Result selectShopMsg(@PathVariable int shopId) throws isPassException {
        log.info("shopId:{}",shopId);
        shopParam shopParam = shopService.selectShopParamById(shopId);
        if(shopParam==null){
            log.info("查询店铺详情失败");
            return Result.error("查询店铺详情失败");
        }else {
            log.info("查询店铺详情成功:{}",shopParam);
            return Result.success(shopParam);
        }
    }

    // 创建店铺
    @PostMapping("/insertShop")
    public Result insertShop(@RequestBody shop shop){
        log.info("正在新建店铺shop:{}",shop);
        if(shop.getName().isEmpty()||shop.getName()==null){
            log.info("店铺名不能为空");
            return Result.error("店铺名不能为空");
        }
        Integer userId = Constant.THREAD_LOCAL.get();
        boolean result = shopService.insertShop(shop, userId);
        if(!result){
            log.info("创建店铺失败");
            return Result.error("创建店铺失败");
        }else {
            log.info("创建店铺成功");
            return Result.success("创建店铺成功");
        }
    }

    //查询用户的全部店铺
    @GetMapping("/selectShop")
    public Result selectShop(){
        log.info("正在查询店铺");
        Integer userId = Constant.THREAD_LOCAL.get();
        List<shop> shopList= (List<shop>) shopService.selectShopList(userId);
        log.info("shopList:{}",shopList);
        if(shopList==null||shopList.isEmpty()){
            log.info("未查询到店铺");
            return Result.success("未查询到店铺");
        }else {
            return Result.success(1,"查询成功",shopList);
        }
    }

    @DeleteMapping("/deleteShop/{shopId}")
    public Result deleteShop(@PathVariable("shopId") Integer shopId) throws DeleteException {
        log.info("正在删除店铺");
        boolean result= shopService.deleteShopById(shopId);
        if(result){
            log.info("删除店铺成功");
            return Result.success("删除店铺成功");
        }else {
            return Result.error("删除店铺失败");
        }
    }






}
